package com.swufestu.hello;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;

public class MyThread implements Runnable{
    private static final String TAG = "MyThread";
    private final static long ONE_DAY_MSECOND = 24 * 60 * 60 * 1000;

    private Handler handler;

    public  void setHanlder(Handler handler){
        this.handler = handler;
    }

    @Override
    public void run() {

        try{
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        Log.i(TAG,"run: ......");

        //用于保存获取的数据
        Bundle bundle = new Bundle();

//        Intent i = new Intent();
//        //创建闹钟对象
//        AlarmManager alarmManager = (AlarmManager)getSystemService(Service.ALARM_SERVICE);
//        PendingIntent pi = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
//        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),ONE_DAY_MSECOND,pi);

        try{
            Document doc = Jsoup.connect("http://www.usd-cny.com/bankofchina.htm").get();
            Log.i(TAG,"run: title=" + doc.title());

//            Elements h4s = doc.getElementsByTag("h4");
//            for(Element h4 : h4s){
//                Log.i(TAG,"run: h4=" + h4.text());
//            }
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.first();

//            Log.i(TAG,"run: table=" + table1);
            //读取某一label的属性
//            Elements hrefs = table1.getElementsByTag("a");
//            for(Element a : hrefs){
//                Log.i(TAG,"run: a=" + a.text());
//            }
            //获取tr元素
            Elements trs = table1.getElementsByTag("tr");
            trs.remove(0);
            for(Element tr : trs){
//                Log.i(TAG, "run: tr=" + tr);
                Elements tds = tr.getElementsByTag("td");
                String cname = tds.get(0).text();
                String cval = tds.get(5).text();
                Log.i(TAG,"run: cname=" + cname + " --> " + cval);

                if("美元".equals(cname)){
                    bundle.putFloat("r1", 100f/Float.parseFloat(cval));
                }else if("欧元".equals(cname)){
                    bundle.putFloat("r2", 100f/Float.parseFloat(cval));
                }else if("泰铢".equals(cname)){
                    bundle.putFloat("r3", 100f/Float.parseFloat(cval));
                }
            }
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        //Bundle中保存获取的数据

        //获取MSG对象，发送消息给主线程
        Message msg = handler.obtainMessage(6,bundle);
//        msg.what = 6;
//        msg.obj = "hello from run";
//        msg.obj = bundle;
        handler.sendMessage(msg);
        Log.i(TAG,"run: 消息已发送");
    }
}
